<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  canvas {
    display: block;
    margin: 0 auto;
    border: 2px solid #aaa;
  }
</style>
<body>
  <canvas id="canvas" height="500px" width="500px">
    您的浏览器版本过低，请升级浏览器或者使用谷歌浏览器打开！
  </canvas>
  <!--
    常用方法和属性
    一、画直线
    1、获取画布
    2、获取画布上下文
    3、开始一条路径
    4、确定起始点
    5、确定结束点
    6、绘制
    7、结束路径
  -->
  <script>
     /** @type {HTMLCanvasElement} */
    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');

    drawLine(100, 100, 105, 100, 'green', 2);
    drawLine(110, 100, 115, 100, 'green', 2);
    drawLine(120, 100, 125, 100, 'green', 2);
    drawLine(130, 100, 135, 100, 'green', 2);
    for (let i = 0; i < 20; i++) {
      drawLine(100 + i * 10, 100, 105 + 10 * i, 100, 'green', 2);
    }

    /**
     * 画直线
     */
    function drawLine(x1, y1, x2, y2, color, width) {
      ctx.beginPath();
      ctx.moveTo(x1, y1);
      ctx.lineTo(x2, y2);
      // strokeStyle设置或返回用于笔触的颜色、渐变或模式。lineWidth设置线宽 在绘制之前设置
      ctx.strokeStyle = color;
      ctx.lineWidth = width;
      ctx.stroke();
      ctx.closePath();
    }
  </script>
</body>
</html>